
package com.trydofor.id.exec;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.trydofor.id.doer.shell.ArgHelp;
import com.trydofor.id.doer.shell.Command;

/**
 * @author : Shi Rongjiu (www.trydofor.com)
 */
public final class SessionCmd {
    
    private SessionCmd() {
    }
    
    /**
     * show server session config
     * 
     * @return the command
     */
    public static Command sessionConfig() {
        return new Command("config", "show server session config");
    }
    
    /**
     * show the session status
     * 
     * @param serverRef the server
     * @return the command
     */
    public static Command sessionStatus(final AtomicReference<NioSocketAcceptor> serverRef) {
        Command cmd = new Command("status", "show the session status") {
            
            @Override
            public void refreshArgs() {
                final ArgHelp arg = new ArgHelp("session-id");
                arg.setRequired(true);
                NioSocketAcceptor server = serverRef.get();
                if (server != null) {
                    arg.setEditable(false);
                    Map<Long, IoSession> ss = server.getManagedSessions();
                    for (Entry<Long, IoSession> e : ss.entrySet()) {
                        IoSession s = e.getValue();
                        arg.addOption(e.getKey() + ":" + s.getRemoteAddress() + "->" + s.getLocalAddress());
                    }
                }
                else {
                    arg.setEditable(true);
                }
                this.addArgument(arg);
            }
        };
        
        return cmd;
    }
    
    /**
     * close the session
     * 
     * @param serverRef the server
     * @return the command
     */
    public static Command closeSession(final AtomicReference<NioSocketAcceptor> serverRef) {
        Command cmd = new Command("close", "close the session") {
            
            @Override
            public void refreshArgs() {
                final ArgHelp arg = new ArgHelp("session-id");
                arg.setRequired(true);
                NioSocketAcceptor server = serverRef.get();
                if (server != null) {
                    arg.setEditable(false);
                    Map<Long, IoSession> ss = server.getManagedSessions();
                    for (Entry<Long, IoSession> e : ss.entrySet()) {
                        arg.addOption(e.getKey() + ":" + e.getValue());
                    }
                }
                else {
                    arg.setEditable(true);
                }
                this.addArgument(arg);
            }
        };
        
        final ArgHelp arg = new ArgHelp("immediately");
        arg.setManual("Closes immediately or after all queued write requests are flushed");
        arg.addOption("true", "false");
        arg.setEditable(false);
        arg.setDefault("false");
        cmd.addArgument(arg);
        
        return cmd;
    }
}
